package github.leavesczy.reactivehttpsamples.ui.addbridge

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import github.leavesczy.reactivehttpsamples.base.BaseFragment
import github.leavesczy.reactivehttpsamples.core.mode.BridgeRecordItem
import github.leavesczy.reactivehttpsamples.core.mode.NeuralNetworkData
import github.leavesczy.reactivehttpsamples.databinding.FragmentPreEvaluationModelBinding
import github.leavesczy.reactivehttpsamples.ui.addbridge.func.BridgeFunction
import github.leavesczy.reactivehttpsamples.util.BridgeDataHelper
import github.leavesczy.reactivehttpsamples.util.exception.BusException


class PreEvaluationModelBridge : BaseFragment(), BridgeFunction {
    companion object {
        fun newInstance(item: BridgeRecordItem?): Fragment {
            val fragment = PreEvaluationModelBridge()
            val bundle = Bundle()
            item?.let {
                bundle.putSerializable("item", it)
            }
            fragment.arguments = bundle
            return fragment
        }
    }

    private var bind: FragmentPreEvaluationModelBinding? = null

    private val mBind: FragmentPreEvaluationModelBinding get() = bind!!

    private var bridgeType: String? = null

    private var itemData: BridgeRecordItem? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        bind = FragmentPreEvaluationModelBinding.inflate(inflater, container, false)
        return mBind.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initListener()
        initRefresh()

        initData()
        initView()
    }


    private fun initView() {
        itemData?.let {
            val editable = it.isCanAnalysis() && !getForbiddenEdit(activity)
            val data = it.neuralNetwork
            dealData(data, editable)

        }
    }

    private fun initData() {
        itemData = arguments?.getSerializable("item") as? BridgeRecordItem

        mBind.icCbGirderType.setItems(BridgeDataHelper.getGirderTypeList())
        mBind.nnCbMainBeamType.setItems(BridgeDataHelper.getMainBeamTypeList())
    }

    fun initListener() {


        mBind.nnTfMainSpan.initButtonTextListener {
            //识别 主跨跨径
            try {
                val value = (activity as? BridgeAddActivity)?.getSpanMax()

                mBind.nnTfMainSpan.editTextStr=value
                showToast("处理完成")
            }catch (e:BusException){
                showToast(e.message?:"处理异常")
            }


        }

        mBind.nnCbMainBeamType.initButtonTextListener {
            //主梁类型
            try {
                val value = (activity as? BridgeAddActivity)?.getMainBeamType()

                mBind.nnCbMainBeamType.setSelectValue(value)
                showToast("处理完成")
            }catch (e:BusException){
                showToast(e.message?:"处理异常")
            }
        }

        mBind.nnTfTowerNumber.initButtonTextListener {
            //桥塔数量
            try {
                val value = (activity as? BridgeAddActivity)?.getTowerNumber()

                mBind.nnTfTowerNumber.editTextStr=value
                showToast("处理完成")
            }catch (e:BusException){
                showToast(e.message?:"处理异常")
            }
        }

        mBind.nnTfTowerHeight.initButtonTextListener {
            //桥塔高度
            try {
                val value = (activity as? BridgeAddActivity)?.getTowerHeightTotal()

                mBind.nnTfTowerHeight.editTextStr=value
                showToast("处理完成")
            }catch (e:BusException){
                showToast(e.message?:"处理异常")
            }
        }
    }

    override fun onResume() {
        super.onResume()
        refreshBridgeType()

    }

    private fun initRefresh() {


    }


    override fun onDestroyView() {
        super.onDestroyView()
        bind = null
    }
    override fun refreshBridgeDetail(itemData: BridgeRecordItem) {
        this.itemData=itemData
        if(bind!=null){
            initView()
        }
    }

    override fun refreshBridgeType() {
        val bridgeType = (activity as? BridgeAddActivity)?.getBridgeType()
        if (bridgeType.isNullOrEmpty()) {
            return
        }
        if (!this.bridgeType.isNullOrEmpty() && this.bridgeType == bridgeType) {
            //不需要重复处理了
            return
        }
        this.bridgeType = bridgeType
        //根据桥梁类型判断 展示的字段
        when (bridgeType) {
            "1" -> {
                showView(mBind.icCbGirderType)
                showView(mBind.nnCbMainBeamType, false)
                showView(mBind.nnTfTowerNumber, false)
                showView(mBind.nnTfTowerHeight, false)
            }


            "3" -> {
                showView(mBind.icCbGirderType, false)
                showView(mBind.nnCbMainBeamType)
                showView(mBind.nnTfTowerNumber)
                showView(mBind.nnTfTowerHeight)
            }
        }

        initDefaultValue()
    }


    private fun initDefaultValue() {
        if (itemData != null) {
            return
        }
        val editable = true
        val data = getDefaultValue()

        dealData(data, editable)
    }

    private fun getDefaultValue(): NeuralNetworkData {
        val bean = NeuralNetworkData(
            girderType = "1",
            mainSpan = "50",
            mainBeamType = "钢箱梁",
            towerNumber = "2",
            towerHeight = "120",
            resultJson = ""
        )
        return when (bridgeType) {
            "1" -> {
                bean.girderType = "1"
                bean.mainSpan = "45"
                bean
            }

            "3" -> {

                bean.mainSpan = "602"
                bean.mainBeamType = "钢混组合梁"
                bean.towerNumber = "2"
                bean.towerHeight = "220"
                bean
            }

            else -> {
                bean
            }

        }
    }

    private fun dealData(data: NeuralNetworkData, editable: Boolean) {
        dealSpinner(mBind.icCbGirderType, data.girderType, editable)
        dealTextButtonField(mBind.nnTfMainSpan, data.mainSpan, editable)
        dealSpinner(mBind.nnCbMainBeamType, data.mainBeamType, editable)
        dealTextButtonField(mBind.nnTfTowerNumber, data.towerNumber, editable)
        dealTextButtonField(mBind.nnTfTowerHeight, data.towerHeight, editable)
    }


    override fun dealRequestParam(mapParent: MutableMap<String, Any>) {

        val map = mutableMapOf<String, Any>()
        mapParent.put("neuralNetwork", map)

        addTextButtonFieldParam(map, mBind.nnTfMainSpan, "mainSpan", "主跨跨径")

        //根据桥梁类型判断 展示的字段
        when (bridgeType) {
            "1" -> {
                addSpinnerParam(map, mBind.icCbGirderType, "girderType", "梁桥类型")
            }

            "3" -> {
                addSpinnerParam(map, mBind.nnCbMainBeamType, "mainBeamType", "主梁类型")
                addTextButtonFieldParam(map, mBind.nnTfTowerNumber, "towerNumber", "桥塔数量")
                addTextButtonFieldParam(map, mBind.nnTfTowerHeight, "towerHeight", "桥塔总高度")
            }
        }
    }

    override fun check() {
        checkTextButton(mBind.nnTfMainSpan, itemData?.neuralNetwork?.mainSpan)
        //根据桥梁类型判断 展示的字段
        when (bridgeType) {
            "1" -> {
                checkSpinner(mBind.icCbGirderType, itemData?.neuralNetwork?.girderType)
            }

            "3" -> {
                checkSpinner(mBind.nnCbMainBeamType, itemData?.neuralNetwork?.mainBeamType)
                checkTextButton(mBind.nnTfTowerNumber, itemData?.neuralNetwork?.towerNumber)
                checkTextButton(mBind.nnTfTowerHeight, itemData?.neuralNetwork?.towerHeight)
            }
        }
    }

}